package com.vehiclemanagement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vehiclemanagement.common.CommonResult;
import com.vehiclemanagement.entity.ETCParking;
import com.vehiclemanagement.service.ETCParkingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * ETC停车记录管理控制器
 */
@RestController
@RequestMapping("/etcParking")
@Slf4j
public class ETCParkingController {

    @Autowired
    private ETCParkingService etcParkingService;

    /**
     * 分页查询ETC停车记录
     */
    @GetMapping("/page")
    public CommonResult<IPage<ETCParking>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String searchTerm,
            @RequestParam(required = false) Long vehicleId,
            @RequestParam(required = false) Long driverId,
            @RequestParam(required = false) Long etcCardId,
            @RequestParam(required = false) Integer parkingType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        IPage<ETCParking> page = etcParkingService.pageParking(current, size, searchTerm, 
                vehicleId, driverId, etcCardId, parkingType, status, startTime, endTime);
        
        return CommonResult.success(page);
    }

    /**
     * 获取ETC停车记录详情
     */
    @GetMapping("/{id}")
    public CommonResult<ETCParking> getById(@PathVariable Long id) {
        ETCParking parking = etcParkingService.getParkingById(id);
        return parking != null ? CommonResult.success(parking) : CommonResult.failed("停车记录不存在");
    }

    /**
     * 添加ETC停车记录
     */
    @PostMapping
    public CommonResult<Boolean> add(@RequestBody ETCParking parking) {
        boolean result = etcParkingService.addParking(parking);
        return result ? CommonResult.success(true, "添加成功") : CommonResult.failed("添加失败");
    }

    /**
     * 修改ETC停车记录
     */
    @PutMapping
    public CommonResult<Boolean> update(@RequestBody ETCParking parking) {
        boolean result = etcParkingService.updateParking(parking);
        return result ? CommonResult.success(true, "修改成功") : CommonResult.failed("修改失败");
    }

    /**
     * 删除ETC停车记录
     */
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> delete(@PathVariable Long id) {
        boolean result = etcParkingService.deleteParking(id);
        return result ? CommonResult.success(true, "删除成功") : CommonResult.failed("删除失败");
    }

    /**
     * 结算ETC停车记录
     */
    @PutMapping("/settle/{id}")
    public CommonResult<Boolean> settle(@PathVariable Long id) {
        boolean result = etcParkingService.settleParking(id);
        return result ? CommonResult.success(true, "结算成功") : CommonResult.failed("结算失败");
    }

    /**
     * 批量结算ETC停车记录
     */
    @PutMapping("/batchSettle")
    public CommonResult<Integer> batchSettle(@RequestBody List<Long> ids) {
        int count = etcParkingService.batchSettleParking(ids);
        return count > 0 ? CommonResult.success(count, "成功结算" + count + "条记录") : CommonResult.failed("结算失败");
    }

    /**
     * 记录ETC停车出场信息
     */
    @PutMapping("/recordExit/{id}")
    public CommonResult<Boolean> recordExit(
            @PathVariable Long id,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime exitTime,
            @RequestParam BigDecimal fee) {
        
        boolean result = etcParkingService.recordExit(id, exitTime, fee);
        return result ? CommonResult.success(true, "记录出场信息成功") : CommonResult.failed("记录出场信息失败");
    }

    /**
     * 获取车辆停车总费用
     */
    @GetMapping("/fee/vehicle/{vehicleId}")
    public CommonResult<BigDecimal> getFeeByVehicleId(@PathVariable Long vehicleId) {
        BigDecimal fee = etcParkingService.sumFeeByVehicleId(vehicleId);
        return CommonResult.success(fee);
    }

    /**
     * 获取ETC卡停车总费用
     */
    @GetMapping("/fee/etcCard/{etcCardId}")
    public CommonResult<BigDecimal> getFeeByEtcCardId(@PathVariable Long etcCardId) {
        BigDecimal fee = etcParkingService.sumFeeByEtcCardId(etcCardId);
        return CommonResult.success(fee);
    }

    /**
     * 获取驾驶员停车总费用
     */
    @GetMapping("/fee/driver/{driverId}")
    public CommonResult<BigDecimal> getFeeByDriverId(@PathVariable Long driverId) {
        BigDecimal fee = etcParkingService.sumFeeByDriverId(driverId);
        return CommonResult.success(fee);
    }

    /**
     * 获取按月统计数据
     */
    @GetMapping("/statistics/month")
    public CommonResult<List<Map<String, Object>>> statisticsByMonth(
            @RequestParam(required = false) Integer year) {
        
        List<Map<String, Object>> statistics = etcParkingService.statisticsByMonth(year);
        return CommonResult.success(statistics);
    }

    /**
     * 获取按停车类型统计数据
     */
    @GetMapping("/statistics/parkingType")
    public CommonResult<List<Map<String, Object>>> statisticsByParkingType(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        List<Map<String, Object>> statistics = etcParkingService.statisticsByParkingType(startTime, endTime);
        return CommonResult.success(statistics);
    }
} 